package cn.hp.consumer.controller;


import cn.hp.consumer.client.UserFeignClient;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("consumer")
//@DefaultProperties(defaultFallback = "defaultFallBack")
public class ConsumerController {
//    @Autowired
//    private RestTemplate restTemplate;

    @Autowired
    private UserFeignClient userFeignClient;


    @GetMapping("user/{id}")
//    @HystrixCommand
    public String queryById(@PathVariable("id") Long id){
//        String url = "http://localhost:8081/user/" + id;

//        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
//        ServiceInstance instance = instances.get(0);
//        String url = String.format("http://%s:%s/user/%s", instance.getHost(), instance.getPort(), id);

//        if(id == 1){
//            throw new RuntimeException("太忙了");
//        }

//        String url = "http://user-service/user/" + id;
//
//        String user = restTemplate.getForObject(url, String.class);
        return userFeignClient.queryById(id);
    }

//    public String defaultFallBack(){
//        return "服务器繁忙，请稍后再试";
//    }

}
